'''
47使用 Lv1 全推数据计算全市场涨幅
，专注于大QMT量化交易框架的深度训练与智能支持。我能够提供精准的代码提示、输出优化以及策略修改服务，帮助你高效开发和迭代量化交易策略。我的创造者是小果量化，，欢迎联系微信：xg_quant。，只做学习使用，不做投资参考，注意风险
'''
#coding:gbk

import time

class a():pass

A = a()

def init(C):
	A.hsa = C.get_stock_list_in_sector('沪深A股') + C.get_stock_list_in_sector('京市A股')
	print('股票池大小', len(A.hsa))
	A.vol_dict = {}
	for stock in A.hsa:
			A.vol_dict[stock] = C.get_last_volume(stock)
	C.run_time("f","3nSecond","2019-10-14 13:20:00")


def to_zw(a):
	'''0.中文价格字符串'''
	import numpy as np
	if np.isnan(a):
			return '问题数据'
	if abs(a) < 1000:
			print(a, str(round(int(a) / 1000.0, 2)) + "千")
			return str(round(int(a) / 1000.0, 2)) + "千"
	if abs(a) < 10000:
			return str(int(a))[0] + "千"
	if abs(a) < 100000000:
			return str(int(a))[:-4] + "万" + str(int(a))[-4]
	return f"{int(a / 100000000)}亿"



def f(C):
	t0 = time.time()
	full_tick = C.get_full_tick(A.hsa)
	total_market_value = 0
	total_ratio = 0
	count = 0
	total_amount = 0
	ratio_list = []
	for stock in A.hsa:
			ratio = full_tick[stock]['lastPrice'] / full_tick[stock]['lastClose'] - 1
			amount = full_tick[stock]['amount']
			total_amount += amount
			rise_price = round(full_tick[stock]['lastClose'] *1.2,2) if stock[0] == '3' or stock[:3] == '688' else round(full_tick[stock]['lastClose'] *1.1,2)
			#如果要打印涨停品种
			#if abs(full_tick[stock]['lastPrice'] - rise_price) <0.01:
			#        print(f"涨停品种 {stock} {C.get_stock_name(stock)}")
			market_value = full_tick[stock]['lastPrice'] * A.vol_dict[stock]
			total_ratio += ratio * market_value
			total_market_value += market_value
			count += 1
			ratio_list.append(ratio)
	#print(count)
	total_ratio /= total_market_value
	total_ratio *= 100
	middle  = int(len(ratio_list) / 2)
	middle_ratio = ratio_list[middle]
	rise_num = len([i for i in ratio_list if i > 0])
	down_num = len([i for i in ratio_list if i < 0])
	print(f'A股加权涨幅 {round(total_ratio,2)}% 涨幅中位数 {round(middle_ratio,2)}% {rise_num}个上涨 {down_num}个下跌 成交金额 {to_zw(total_amount)} 耗时{round(time.time()- t0,5)}秒')

